﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Web.UI;

namespace Royaumes_Libres.Database
{
    /// <summary>
    /// 
    /// </summary>
    /// <remarks>Patrick Chabot</remarks>
    public class cBuildingsAdmin
    {
        protected int pPkBuilding;
        protected string pName;
        protected int pMana;
        protected int pTimber;
        protected int pOre;
        protected int pWorkUnite;
        protected int pWorkMax;
        protected decimal pBonusMana;
        protected decimal pBonusTimber;
        protected decimal pBonusOre;
        protected decimal pBonusFood;
        protected decimal pBonusPopulation;
        protected bool pUniqueBuild;
        protected int pFkImage;
        protected string pImgUrl;

        public int PkBuilding { get { return this.pPkBuilding; } }
        public string Name { get { return this.pName; } }
        public int Mana { get { return this.pMana; } set { this.pMana = value; } }
        public int Timber { get { return this.pTimber; } set { this.pTimber = value; } }
        public int Ore { get { return this.pOre; } set { this.pOre = value; } }
        public decimal BonusMana { get { return this.pBonusMana; } set { this.pBonusMana = value; } }
        public decimal BonusTimber { get { return this.pBonusTimber; } set { this.pBonusTimber = value; } }
        public decimal BonusOre { get { return this.pBonusOre; } set { this.pBonusOre = value; } }
        public decimal BonusFood { get { return this.pBonusFood; } set { this.pBonusFood = value; } }
        public decimal BonusPopulation { get { return this.pBonusPopulation; } set { this.pBonusPopulation = value; } }
        
        public string ImgUrl { get { return this.pImgUrl; } }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        /// <remarks>Patrick Chabot</remarks>
        public ucBuildingAdmin GetControl(Page page)
        {
            ucBuildingAdmin uc = (ucBuildingAdmin)page.LoadControl("ucBuildingAdmin.ascx");
            uc.Building = this;
            return uc;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <remarks>Patrick Chabot</remarks>
        public void SaveBuilding()
        {
            SqlCommand cmd = cSQL.GetDbJeuCommand();
            cmd.Connection.Open();

            cmd.CommandText = "cde.spSetBuildingsAdmin";
            cmd.Parameters.Add(cSQL.Param("@PkBuilding", this.pPkBuilding, SqlDbType.Int));
            cmd.Parameters.Add(cSQL.Param("@Name", this.pName, SqlDbType.VarChar));
            cmd.Parameters.Add(cSQL.Param("@Mana", this.pMana, SqlDbType.Int));
            cmd.Parameters.Add(cSQL.Param("@Timber", this.pTimber, SqlDbType.Int));
            cmd.Parameters.Add(cSQL.Param("@Ore", this.pOre, SqlDbType.Int));
            cmd.Parameters.Add(cSQL.Param("@WorkUnite", this.pWorkUnite, SqlDbType.Int));
            cmd.Parameters.Add(cSQL.Param("@WorkMax", this.pWorkMax, SqlDbType.Int));
            cmd.Parameters.Add(cSQL.Param("@BonusMana", this.pBonusMana, SqlDbType.Decimal));
            cmd.Parameters.Add(cSQL.Param("@BonusTimber", this.pBonusTimber, SqlDbType.Decimal));
            cmd.Parameters.Add(cSQL.Param("@BonusOre", this.pBonusOre, SqlDbType.Decimal));
            cmd.Parameters.Add(cSQL.Param("@BonusFood", this.pBonusFood, SqlDbType.Decimal));
            cmd.Parameters.Add(cSQL.Param("@BonusPopulation", this.pBonusPopulation, SqlDbType.Decimal));
            cmd.Parameters.Add(cSQL.Param("@UniqueBuild", this.pUniqueBuild, SqlDbType.Bit));
            cmd.Parameters.Add(cSQL.Param("@FkImage", this.pFkImage, SqlDbType.Int));
            cmd.ExecuteNonQuery();

            cmd.Connection.Close();
        }

        /// <summary>
        /// Pour avoir la liste de tout les buildings
        /// </summary>
        /// <returns></returns>
        /// <remarks>Patrick Chabot</remarks>
        public static cBuildingsAdmin[] GetBuildings()
        {
            List<cBuildingsAdmin> lst = new List<cBuildingsAdmin>();
            SqlCommand cmd = cSQL.GetDbJeuCommand();
            cBuildingsAdmin building;

            try
            {
                cmd.Connection.Open();
                cmd.CommandText = "cde.spGetBuildingsAdmin";
                SqlDataReader dr = cmd.ExecuteReader();

                while (dr.Read())
                {
                    building = new cBuildingsAdmin();

                    building.pPkBuilding = dr.GetInt32(0);
                    building.pName = dr.GetString(1);
                    building.pMana = dr.GetInt32(2);
                    building.pTimber = dr.GetInt32(3);
                    building.pOre = dr.GetInt32(4);
                    building.pWorkUnite = dr.GetInt32(5);
                    building.pWorkMax = dr.GetInt32(6);
                    building.pBonusMana = dr.GetDecimal(7);
                    building.pBonusTimber = dr.GetDecimal(8);
                    building.pBonusOre = dr.GetDecimal(9);
                    building.pBonusFood = dr.GetDecimal(10);
                    building.pBonusPopulation = dr.GetDecimal(11);
                    building.pUniqueBuild = dr.GetBoolean(12);
                    building.pFkImage = dr.GetInt32(13);
                    building.pImgUrl = dr.GetString(14);

                    lst.Add(building);
                }
                cmd.Connection.Close();
                return lst.ToArray();
            }
            catch (Exception ex)
            {
                cmd.Connection.Close();
                throw new Exception("cBuildingsAdmin.GetBuilding() : " + ex.Message);
            }
        }
    }
}